/*This do file takes several party specific loser results files and averages them by numvotes to create a congress specific results file for one
particular country.  This do file works correctly even for countries with data on only one congress*/

clear

set memory 110m 

set matsize 800

set maxvar 32766 /*maximum variable size for Stata 7.0 is 2047. if you are running stata 7.0, change "set maxvar" to 2047*/
set more off



				/* Now, we will establish several macros to run the code.  Each macro will be defined at the

				beginning of the code.  When a macro appears throughout the code, it will be surrounded by 

				single quotation marks, such as `macro'.  Once the macros are defined at the beginning of the 

				code, they do not need to be defined again.  There exists only one other place within the code

				that must be adjusted per each data set.  That place is set off with a string of asterisks, 

				and directions follow the asterisks. */


			/* To adapt this do file for other countries, you must do the following: 
					1)  Run the mastercode.do file and generate a proper results file for each master dataset  
					2)  Change the name of the results file from "country" "year" final.dta to "country"final#.dta, with # being 1 for the results file 
						for the earliest congress, 2 for the next congress, etc until the final congress
					3)  Change local country to the name of the country you are using
					4)  Change local numfiles to the number of results files for the country you are using
			*/






local country "Israel"		/* "country" is the name of the country in the data set */

local numfiles "2"				/* "numfiles" is the number of chambers that have their own results file*/


local results "C:\output\loser/`country'loser"		/*"results" is the basic name of the data files you will be using, minus the number at the end*/ 


local path "c:\temp\"

				/* "path" is the basic path name for all temporary files that this data set will be creating or accessing. 

				It does not name a file, and has no extension.  These files are only useful for error checking once your output has been generated*/

local output "c:\output\loser\country\"

		/* "output" is the basic path name for all final output files that will be created */

local unityricefinal "c:\output/`country'final"



forv j=1/`numfiles'{





/* The following code uses each results file to generate an average for each index in each congress.  It counts the number of parties for which the specific
index was present in the results file, and uses this number to create an average for the congress.  It then saves the averages for each results file in a 
temp file called "country"avgs"results file#".dta */

use `unityricefinal'`j'.dta, replace
keep numvotes
merge using `results'`j'
drop _merge

count if party~=.
local numparties=r(N)
egen sumunitychwp=sum(uloser)
gen avgunitychwp=sumunitychwp/`numparties'

drop uloser

egen sumricechwp=sum(rloser)
gen avgricechwp=sumricechwp/`numparties'

drop rloser

egen sumperwon=sum(perwon)
gen avgperwon=sumperwon/`numparties'

drop perwon

egen sumricerolled=sum(ricerolled)
gen avgricerolled=sumricerolled/`numparties'

drop ricerolled

egen sumricestuffed=sum(ricestuffed)
gen avgricestuffed=sumricestuffed/`numparties'

drop ricestuffed


egen sumunityrolled=sum(unityrolled)
gen avgunityrolled=sumunityrolled/`numparties'

drop unityrolled

egen sumunitystuffed=sum(unitystuffed)
gen avgunitystuffed=sumunitystuffed/`numparties'

drop unitystuffed

keep in 1

keep party avg* numv*

save "`path'\`country'avgs`j'.dta", replace
}

/* The following code merges all of the avgs files into one file */


use "`path'\`country'avgs1.dta", replace

forv j=2/`numfiles'{

append using "`path'\`country'avgs`j'.dta",
}
save "`path'\`country'avgs.dta", replace

/* The following code takes the average indices across all congresses, and weights them by sumClose in order to come up with one weighted average
index for each country */


egen totalnumvotes=sum(numvotes)

gen newunitychwp=avgunitychwp*(numvotes/totalnumvotes)
egen meanuloser=sum(newunitychwp)

gen newricechwp=avgricechwp*(numvotes/totalnumvotes)
egen meanrloser=sum(newricechwp)

gen newperwon=avgperwon*(numvotes/totalnumvotes)
egen meanperwon=sum(newperwon)

gen newricestuffed=avgricestuffed*(numvotes/totalnumvotes)
egen meanricestuffed=sum(newricestuffed)

gen newricerolled=avgricerolled*(numvotes/totalnumvotes)
egen meanricerolled=sum(newricerolled)

gen newunitystuffed=avgunitystuffed*(numvotes/totalnumvotes)
egen meanunitystuffed=sum(newunitystuffed)

gen newunityrolled=avgunityrolled*(numvotes/totalnumvotes)
egen meanunityrolled=sum(newunityrolled)

keep mean* totalnumvotes

keep in 1

gen str20 country = "`country'"

order  country meanperwon meanuloser meanunitystuffed meanunityrolled meanrloser meanricestuffed meanricerolled totalnumvotes

save "`output'\`country'countryloser.dta", replace

